<?php

namespace App\Supports\BankCard\Traits;

use Carbon\Carbon;
use Illuminate\Support\Str;
use Zhuzhichao\BankCardInfo\BankCard as BankCardInterface;
use Illuminate\Validation\ValidationException;


trait HasUserBankCard
{
	
	public function addBnakCard()
	{

		 $result = file_get_contents("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=".request()->bank_card."&cardBinCheck=true");
        $result = json_decode($result);
       	$bankList = BankCardInterface::getBankList();

       	if($result->validated == false){
			fail('银行卡号错误！');

       	}
       	if(!isset($bankList[$result->bank])){

			fail('该银行暂不支持，请绑定其他银行卡');
       	}
		$card_info = BankCardInterface::info(request()->bank_card);
		if(!$card_info['validated']){
			throw ValidationException::withMessages(['err' => '卡号不正确']);
		}
		$user = $this;
		$bank_card = $user->update([
			'bank_abbr' => $card_info['bank'],
			'bank_name' => $card_info['bankName'],
			'cert_number' => request()->bank_card,
			'real_name' => request()->real_name,
		]);
		return $user;
	}
	
}
